package com.sosee.cgfw.friends;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sosee.cgfw.R;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.Utils;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.RongIM;

/**
 * Created by Thinkpad on 2016/3/4.
 * 好友信息
 */
public class FriendsInfos extends Activity implements View.OnClickListener {

    private HttpUtils httpUtils;
    private TextView my_name;
    private TextView my_nickname;
    private RelativeLayout setting_info;
    private TextView my_tel;
    private TextView my_company;
    private  TextView my_duty;
    private Button my_message;
    private ImageButton my_delete;
//    List<FriendsListData.Data> datas;
    private String stuta;

    public String id;
    private  Intent intent;
    private ImageButton  friends_back14;
    private  FriendsInfoData friendsInfoData;

    List<String> cityName = new ArrayList<String>();

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friends_infos);
        intent = getIntent();
        id = intent.getStringExtra("idss");

        httpUtils = new HttpUtils();
        System.out.println("这是接收过来的好友id" + id);

        initData();

    }

    private void initData() {
        my_name = (TextView) findViewById(R.id.my_names);
        my_nickname = (TextView) findViewById(R.id.my_nicknames);
        my_tel = (TextView) findViewById(R.id.my_tels);
        my_company = (TextView) findViewById(R.id.my_companys);
        my_duty = (TextView) findViewById(R.id.my_dutys);

        friends_back14= (ImageButton)findViewById(R.id.friends_back14);


        my_message = (Button) findViewById(R.id.my_messages);

        //拿到这个id去好友列表遍历，如果有就发送消息，没有就显示加为好友。
        String dd = SharedPreferencesUtils.getString(this, "dd");
        Gson gson = new Gson();
        FriendsListData friendsListData = gson.fromJson(dd, FriendsListData.class);
        List<FriendsListData.Data> data = friendsListData.getData();

        if(null!=data){
            for (int i = 0; i < data.size(); i++) {
                String ID = data.get(i).getFriends().getID();
                //id存数组
                cityName.add(ID);
            }
        }


        for(int i = 0; i < cityName.size(); i++){
            System.out.println("好友id"+cityName.get(i));
        }


        initView();

        friends_back14.setOnClickListener(this);
        my_message.setOnClickListener(this);


    }

    private void initView() {

        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.GETUSERINFO_URL1+id, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                String result = responseInfo.result;
                System.out.println("这是个人信息：" + result);

                Gson gson = new Gson();
                friendsInfoData = gson.fromJson(result, FriendsInfoData.class);

                if(null!=result){

                    my_name.setText(friendsInfoData.getRealname()); ;//真实姓名
                    my_nickname.setText(friendsInfoData.getNickname());
                    String name=null;
                    if(friendsInfoData.getPhone().length()>3){
                        name=friendsInfoData.getPhone().substring(0,3)+"...";
                    }else{
                        name=friendsInfoData.getPhone();
                    }
                    my_tel.setText(name);
                    my_company.setText(friendsInfoData.getCompany());
                    my_duty .setText(friendsInfoData.getDuty());

                    System.out.println("被点击id = "+friendsInfoData.getID());

                    for(int i = 0; i < cityName.size(); i++){
                        System.out.println("好友id = "+cityName.get(i));
                    }

                    int nFlag = 0;
                    for(int i=0;i<cityName.size();i++) {

                        if (id.equals(cityName.get(i))) {
                            nFlag = 1;
                            break;
                        }
                    }


                    if(nFlag == 1){
                        my_message.setText("发送消息");

                    }else{
                        my_message.setText("加为好友");
                    }

                }else{
                    System.out.println(friendsInfoData.getStatus());
                }
            }
            @Override
            public void onFailure(HttpException e, String s) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.friends_back14 :
                //关闭
                finish();
                break;


            case R.id.my_messages :


                if("加为好友".equals(my_message.getText().toString())){


                    httpUtils.send(HttpRequest.HttpMethod.GET, Constans.ADDFRIENDS_URL +Constans.userID+ "&friendsID=" +id, new RequestCallBack<String>() {
                        @Override
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            String s = responseInfo.result;
                            System.out.println("这是返回的值" + s);
                            //发送消息到主线程通知更改  加为好友为等待验证。
                            Utils.showToast("请求已经发送");
                            finish();

//
                        }

                        @Override
                        public void onFailure(HttpException e, String s) {

                            Utils.showToast("网络获取异常");
                        }
                    });


                }else{
                    if (RongIM.getInstance() != null) {
                    RongIM.getInstance().startPrivateChat(FriendsInfos.this, id, "hello");
                }

                    }

                break;


        }

    }
}
